package main

import (
	"encoding/json"
	"fmt"
	"golang_note/day08_http/gin/oss"
	"golang_note/day08_http/gin/rabbitmq"
)

//ProcessTransfer 处理文件转移的真正逻辑
func ProcessTransfer(msg []byte) bool {
	//1、解析msg
	pubData := rabbitmq.TransferData{}
	err := json.Unmarshal(msg, &pubData)
	if err != nil {
		fmt.Println("err", err)
		return false
	}
	bucket, err := oss.Client.Bucket("dashers")
	if err != nil {
		fmt.Println("bucket failed", err)
		return false
	}
	//2、将文件上传到oss
	err = bucket.PutObjectFromFile(pubData.DestLocation, pubData.CurLocation)
	if err != nil {
		fmt.Println("put file failed", err)
		return false
	}
	// err = os.RemoveAll(path.Join("./data/", upid))
	if err != nil {
		fmt.Println("remove failed", err)
		return false
	}
	fmt.Println("上传oss成功", pubData.DestLocation)
	return true
}

func main() {
	fmt.Println("开始监听转移任务队列")
	rabbitmq.StartConsume(rabbitmq.TransOSSQueueName, rabbitmq.TransExchangeName, ProcessTransfer)
}
